//
//  PhotoEditVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/9/7.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "PhotoEditVC.h"
#import "DecimalField.h"
#import "IGWorkUpdateApi.h"
@interface PhotoEditVC ()<UIAlertViewDelegate>
@property (strong, nonatomic) DecimalField *priceTF;
@property (strong, nonatomic) UITextField *kucTF;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (strong, nonatomic) IGWorkUpdateApi *api;
@end

@implementation PhotoEditVC

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.title = @"公开图片编辑";
    self.title = NSLocalizedString(@"Edit a Public Picture", nil);
    _sureBtn.layer.cornerRadius = 22;
    
    self.priceTF = [[DecimalField alloc] initWithFrame:CGRectMake(KSCREENWIDTH - 170, 124, 150, 30)];
    self.priceTF.borderStyle = UITextBorderStyleNone;
    self.priceTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.priceTF.font = [UIFont systemFontOfSize:15];
    self.priceTF.placeholder = @"0.0";
    self.priceTF.textColor = [UIColor c5Color];
    self.priceTF.text = _price;
    self.priceTF.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:self.priceTF];
    [self.priceTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    self.kucTF = [[UITextField alloc]initWithFrame:CGRectMake(_priceTF.x, 175, 150, 30)];
    _kucTF.textAlignment = NSTextAlignmentRight;
    _kucTF.borderStyle = UITextBorderStyleNone;
    _kucTF.keyboardType = UIKeyboardTypeNumberPad;
    _kucTF.font = [UIFont systemFontOfSize:15];
    _kucTF.textColor = [UIColor c5Color];
//    _kucTF.placeholder = @"请输入数量";
    _kucTF.placeholder = NSLocalizedString(@"Quantity", nil);
    [_kucTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    _kucTF.text = self.kuc;
    [self.view addSubview:_kucTF];
    self.api = [IGWorkUpdateApi new];
}

- (void)textChange:(UITextField *)sender
{
    if ([sender isEqual:_priceTF])
    {
        _price = _priceTF.text;
        return;
    }
    else if ([sender isEqual:_kucTF])
    {
        _kuc = _kucTF.text;
    }
}

- (IBAction)sureBtnClick:(id)sender
{
    if (self.price.length == 0)
    {
//        [self presentMessageTips:@"请输入售价"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Price", nil)];
        return;
    }
    if (self.kuc.length == 0)
    {
//        [self presentMessageTips:@"请输入库存"];
        [self presentMessageTips:NSLocalizedString(@"Please add inventory amount", nil)];
        return;
    }
    if (self.kuc != nil && ![Tool isIntNumber:self.kuc])
    {
//        [self presentMessageTips:@"数量填写错误"];
        [self presentMessageTips:NSLocalizedString(@"Wrong Quantity", nil)];
        return;
    }
    self.api.worksId = _worksId;
    self.api.price = _price;
    self.api.stock = _kuc;
    [self.api startWithDecodeClass:[IGWorkUpdateResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            self.block(self.price, self.kuc);
            [self.view endEditing:YES];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];    
}

- (void)ig_leftBackButtonPressed:(id)sender
{
    [self.view endEditing:YES];
//    [[[UIAlertView alloc] initWithTitle:@"提示" message:@"确定放弃更改, 返回上一界面?"
//                               delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Notice", nil) message:NSLocalizedString(@"Discard changes and go back?",nil)
                               delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Confirm", nil), nil] show];
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1)
    {
        [self.view endEditing:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}




@end
